
package com.cmc.dao.hibernate;

import java.util.List;

import com.cmc.dao.hibernate.BaseDaoHibernate;
import com.cmc.model.PersonalRest;
import com.cmc.dao.PersonalRestDao;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.HibernateCallback;

public class PersonalRestDaoHibernate extends BaseDaoHibernate implements PersonalRestDao {

    /**
     * @see com.cmc.dao.PersonalRestDao#getPersonalRests(com.cmc.model.PersonalRest)
     */
    public List getPersonalRests(final PersonalRest personalRest) {
        //return getHibernateTemplate().find("from PersonalRest");

        /* Remove the line above and uncomment this code block if you want 
           to use Hibernate's Query by Example API. */
        if (personalRest == null) {
            return getHibernateTemplate().find("from PersonalRest");
        } else {
            // filter on properties set in the personalRest
            HibernateCallback callback = new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException {
                    Example ex = Example.create(personalRest).ignoreCase().enableLike(MatchMode.ANYWHERE);
                    return session.createCriteria(PersonalRest.class).add(ex).list();
                }
            };
            return (List) getHibernateTemplate().execute(callback);
        }
    }

    /**
     * @see com.cmc.dao.PersonalRestDao#getPersonalRest(Long id)
     */
    public PersonalRest getPersonalRest(final Long id) {
        PersonalRest personalRest = (PersonalRest) getHibernateTemplate().get(PersonalRest.class, id);
        if (personalRest == null) {
            log.warn("uh oh, personalRest with id '" + id + "' not found...");
            throw new ObjectRetrievalFailureException(PersonalRest.class, id);
        }

        return personalRest;
    }

    /**
     * @see com.cmc.dao.PersonalRestDao#savePersonalRest(PersonalRest personalRest)
     */    
    public void savePersonalRest(final PersonalRest personalRest) {
        getHibernateTemplate().saveOrUpdate(personalRest);
    }

    /**
     * @see com.cmc.dao.PersonalRestDao#removePersonalRest(Long id)
     */
    public void removePersonalRest(final Long id) {
        getHibernateTemplate().delete(getPersonalRest(id));
    }
}
